package cn.o.generate.controller;

import cn.hutool.core.io.IoUtil;
import cn.o.generate.service.GeneratorService;
import cn.o.generate.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 代码生成器
 *
 * @author teler
 */
@Controller
@RequestMapping("/generator")
public class GeneratorController {
	@Autowired
	private GeneratorService generatorService;

	/**
	 * 列表
	 */
	@ResponseBody
	@RequestMapping("/list")
	public R list(){
        List<Map<String, Object>> list = generatorService.queryList();

		return R.ok().put("data",list);
	}

	/**
	 * 生成代码
	 */
	@RequestMapping("/code")
	public void code(String tables, HttpServletResponse response) throws IOException{
		byte[] data = generatorService.generatorCode(tables.split(","));

		response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=\"code.zip\"");
        response.addHeader("Content-Length", "" + data.length);
        response.setContentType("application/octet-stream; charset=UTF-8");

        IoUtil.write(response.getOutputStream(),true,data);
	}
}
